package com.woniu.backparking.jwtUtil;


import org.apache.shiro.codec.Base64;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.io.UnsupportedEncodingException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author HashSong
 * @ClassName: ShiroConfig
 * @Description:
 * @date 2021/4/28 21:49
 * @Version 1.0
 */
@Configuration
public class ShiroConfig {
    @Bean
    public JWTRealm initUserRealm(){
        return new JWTRealm();
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilter() throws UnsupportedEncodingException {
        //实例化Shiro过滤器工厂
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        //在工厂中注入安全管理器
        shiroFilterFactoryBean.setSecurityManager(initSecurityManager());
        //创建一个有序键值对用于存储黑白名单
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String, String>();

        Map<String, Filter> filters = shiroFilterFactoryBean.getFilters();
        filters.put("jwt",new JWTFileter());
        //anon表示无须登陆就能访问的资源地址
        filterChainDefinitionMap.put("/admin/login", "anon");
        filterChainDefinitionMap.put("/**", "jwt");
        //将黑白名单配置到shiro过滤器
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    @Bean
    public CookieRememberMeManager initCookieRememberMeManager(){
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        SimpleCookie rememberMe = new SimpleCookie("rememberMe");
        rememberMe.setMaxAge(7*24*60*60);
        cookieRememberMeManager.setCookie(rememberMe);
        //设置加密秘钥
        cookieRememberMeManager.setCipherKey(Base64.decode("Woniuxywuyanzu520niubi=="));
        return cookieRememberMeManager;
    }
//将Cookie管理器添加到安全管理器中
    @Bean
    public SecurityManager initSecurityManager(){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(initUserRealm());
        securityManager.setRememberMeManager(initCookieRememberMeManager());
        return securityManager;
    }
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
        AuthorizationAttributeSourceAdvisor advisor=new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(initSecurityManager());
        return advisor;
    }
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator app=new DefaultAdvisorAutoProxyCreator();
        app.setProxyTargetClass(true);
        return app;
    }
}
